package shooter.sound;

import java.net.URL;
import java.util.HashMap;

import shooter.Shooter;

/**
 * Loads resources.
 * 
 * @author Michail Tausch
 */
public abstract class ResourceCache {
	@SuppressWarnings("unchecked")
	protected HashMap resources;

	/**
	 * The constructor.
	 */
	@SuppressWarnings("unchecked")
	public ResourceCache() {
		resources = new HashMap();
	}
	
	/**
	 * Returns a object of a resource.
	 * @param name
	 * @return
	 */
	protected Object loadResource(String name) {
		URL url = null;
		url = Shooter.class.getResource(name);
		return loadResource(url);
	}

	/**
	 * Returns a object of a resource.
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected Object getResource(String name) {
		Object res = resources.get(name);
		if (res == null) {
			res = loadResource(name);
			resources.put(name, res);
		}
		return res;
	}
	
	/**
	 * Returns a object of a resource.
	 * @param url
	 * @return
	 */
	protected abstract Object loadResource(URL url);

}